import datetime

from PySide2.QtCore import QThread, QObject, Signal, Slot, QWaitCondition, QMutex, QDeadlineTimer
from PySide2.QtWidgets import QApplication, QInputDialog, QDialog, QLineEdit, QPushButton, QVBoxLayout, QLabel
import threading

class MyInputDialog(QDialog):
    def __init__(self):
        super().__init__()

        #
        # 输入框
        #
        self.input = QLineEdit()

        #
        # label 标签
        #
        self.label_1 = QLabel()

        #
        # label_2
        #
        self.label_2 = QLabel()

        #
        # 发送按钮
        #
        self.ok_btn = QPushButton(text="发送")
        self.ok_btn.clicked.connect(self.submit)

        layout = QVBoxLayout()
        layout.addWidget(self.input)
        layout.addWidget(self.label_1)
        layout.addWidget(self.label_2)
        layout.addWidget(self.ok_btn)
        self.setLayout(layout)

    @Slot(str)
    def update_label1(self, result):
        print('in parent @Slot function, thread = ' + str(threading.current_thread()))
        self.label_1.setText("更新标签成功：" + result)

    @Slot(str)
    def update_label2(self, result):
        self.label_2.setText("更新标签成功：" + result)

    def submit(self):
        print('in ui clicked connected function, thread = ' + str(threading.current_thread()))
        msg = self.input.text()
        message.append(msg)


class Worker(QThread):
    def __init__(self, parent):
        super(Worker, self).__init__()
        self.signals = Communicate()
        self.parent = parent
        self.signals.label_singal.connect(parent.update_label1)
        self.signals.table_singal.connect(self.update_label2)
        self._mutex = QMutex()
        self._con = QWaitCondition()

    @Slot(str)
    def update_label2(self, result):
        print('in QThread @Slot function, thread = ' + str(threading.current_thread()))
        self.parent.label_2.setText("更新标签成功：" + result)

    def run(self):
        while True:
            if len(message) != 0:
                print('in QThread run loop, thread = ' + str(threading.current_thread()))
                msg = message.pop(0)
                if msg == "1":
                    self._mutex.lock()
                    # 测试一下WaitCondition用法
                    self._con.wait(self._mutex, 2000)
                    self._mutex.unlock()
                    self.get_label1()
                elif msg == "2":
                    self.get_label2()

    def get_label1(self):
        #
        # 模拟耗时操作，去获取数据
        #
        self.signals.label_singal.emit(get_time())

    def get_label2(self):
        self.signals.table_singal.emit(get_time())


class Communicate(QObject):
    label_singal = Signal(str)
    table_singal = Signal(str)


def get_time() -> str:
    """
    获取当前时间，返回时间字符串，如：2020-06-12 11:13:22
    :return:时间字符串
    """
    now_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    return now_time


#
# 消息队列，全局变量
#
message = []

if __name__ == '__main__':
    app = QApplication()

    #
    # 显示输入框
    #
    dial = MyInputDialog()
    dial.show()

    worker = Worker(dial)
    worker.start()

    app.exec_()
